package com.sec.android.app.myfiles.module.preview;

import android.content.Context;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.navigation.NavigationManager;

import java.util.ArrayList;

public class PreviewCompressMenuImp extends AbsMenuImp {

    public PreviewCompressMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        setMenuItemVisibility(menu, R.id.menu_extract, true);
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
    }

    @Override
    public boolean _onOptionsItemSelected(MenuItem item) {
        boolean ret = false;
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_extract:
                NavigationInfo info = mFragment.getNavigationInfo();
                FileRecord record = info.getCurRecord();
                if (record instanceof CompressFileRecord) {
                    MyFilesFacade.decompress(mFragment.getProcessId(), mContext, mFragment, ((CompressFileRecord) record).getArchiveRecord(),
                            mFragment.getSelectedFile());
                }
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.UNZIP_POPUP, SamsungAnalyticsLog.Event.UNZIP, null);
                ret = true;
                break;
            case R.id.menu_cancel:
                int processId = mFragment.getProcessId();
                NavigationInfo navigationInfo = NavigationManager.getInstance(processId).leave(mContext);
                if (navigationInfo == null) {
                    if (AppFeatures.isNoFocusTabletUI()) {
                        MyFilesFacade.goHomeBlank(processId, null);
                    } else {
                        mFragment.getActivity().finish();
                    }
                }
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.UNZIP_POPUP, SamsungAnalyticsLog.Event.CANCEL, null);
                ret = true;
                break;
        }

        if (!ret) {
            ret = super._onOptionsItemSelected(item);
        }

        return ret;
    }

    @Override
    public String getSelectedItemString(int selectedCnt) {
        String ret = null;
        if (selectedCnt >= 0) {
            ret = super.getSelectedItemString(selectedCnt);
        }
        return ret;
    }

    @Override
    public void createContextualMenu(ContextMenu menu, AbsMyFilesFragment fragment, FileRecord record) {
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        return false;
    }

    @Override
    protected void _onUpdateSelectLayoutMenu(Menu menu, NavigationMode navigationMode) {
        if (navigationMode == NavigationMode.Preview_compress_item) {
            boolean selectedFileExist = mFragment.getSelectedFileCount() > 0;
            setMenuItemVisibility(menu, R.id.menu_extract, selectedFileExist);
        }
    }
}
